/*
    RExLib project - RTcpSocket_Example, file main.cpp
    Copyright (C) 2012  Sarvaritdinov Ravil ra9oaj@gmail.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdlib.h>
#include <stdio.h>
#include <iostream>

#include "../../network/rtcpsocket.h"

using namespace rex;

int main ( int argc, char * argv[] )
{
    RTcpSocket sock;
    int res = 0;
    if ( argc < 3 )
        res = sock.connectToHost ( "www.yandex.ru",80 );
    else
        res = sock.connectToHost ( argv[1],atoi ( argv[2] ) );

    if ( res )
    {
        printf ( "Connection to host '%s' (IP: %s) on port %d\r\n",sock.host(),sock.host ( RTcpSocket::RS_NUMERIC ),sock.port() );
        char *inbuf = ( char* ) malloc ( 1024 );
        char outbuf[] = "GET / HTTP/1.0\r\n\r\n";
        sock.write ( outbuf,sizeof ( outbuf ) );
        size_t rsize = 0;
        while ( ( rsize = sock.read ( inbuf,1024 ) ) > 0 )
        {
            fwrite ( inbuf,rsize,1,stdout );
            bzero ( inbuf,1024 );
        }
        free ( inbuf );
    }
    else std::cout << "Error connection to host!" << std::endl;
    std::cout << std::endl;
    sock.closeSocket();

    return 0;
}
